﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.User;
using Enterprise.Service.User;
using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;
using Enterprise.Service.Department;
namespace Enterprise.UI.Web.Modules.UserConfig
{
    public partial class UserWfConfigManage : Enterprise.Service.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnBindUser();
            OnBindDate();
            OnData();
        }
        private void OnBindUser()
        {
            DepartmentService dService = new DepartmentService();
            var list = dService.DepartmentList().OrderBy(p =>p.DORDERBY);
            tb_Dept.DataSource = list;
            tb_Dept.DataTextField = "DNAME";
            tb_Dept.DataValueField = "DEPTID";
            tb_Dept.DataBind();
            tb_Dept.Items.Insert(0, new ListItem("请选择部门", "0"));
        }

        private void OnBindDate()
        {
            //默认时间为半个月时间
            tb_EndTime.Text = DateTime.Now.AddDays(15).ToShortDateString();
        }

        private void OnData()
        {
            UserWfConfigService cfService = new UserWfConfigService();
            var query = cfService.UserWfConfig(Utility.Get_UserId);
            var qGw = query.Where(p => p.WFTYPEID == 1).FirstOrDefault();
            if (qGw != null)
            {
                ListItem li = tb_User.Items.FindByValue(qGw.SQUSERID.ToString());
                if (li != null)
                    li.Selected = true;

                tb_EndTime.Text = qGw.ENDTIME.ToShortDateString();
            }
        }

        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
             string _note = "操作成功";
             try
             {
                 UserWfConfigService cfService = new UserWfConfigService();
                 //首先删除该用户的所有授权
                 UserWfConfigEntity entityOld = new UserWfConfigEntity();
                 entityOld.USERID = Utility.Get_UserId;
                 entityOld.DB_Option_Action = "DeleteUserId";
                 cfService.UserWfConfigExecute(entityOld);

                 //新增公文流转
                 UserWfConfigEntity entityGw = new UserWfConfigEntity();
                 entityGw.DB_Option_Action = "Insert";
                 entityGw.USERID = Utility.Get_UserId;
                 entityGw.WFTYPEID = 1;
                 entityGw.SQUSERID = (int)Utility.sink(tb_User.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
                 entityGw.STARTIME = DateTime.Now;
                 entityGw.ENDTIME = (DateTime)Utility.sink(tb_EndTime.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                 entityGw.ISUSED = 1;
                 cfService.UserWfConfigExecute(entityGw);
             }
             catch (Exception ex)
             {
                 _note = ex.Message;
             }
             Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "UserWfConfig.aspx");

        }

        protected void tb_Dept_SelectedIndexChanged(object sender, EventArgs e)
        {
            tb_User.Items.Clear();
            int deptId = (int)Utility.sink(tb_Dept.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            if (deptId != 0)
            {
                UserService uService = new UserService();
                var list = uService.UserList().Where(p => p.DEPTID == deptId).OrderBy(p => p.UORDERBY);
                tb_User.DataSource = list;
                tb_User.DataTextField = "UNAME";
                tb_User.DataValueField = "USERID";
                tb_User.DataBind();
                tb_User.Items.Insert(0, new ListItem("请选择人员", "0"));
            }
        }

    }
}